package org.jackysoft.struts2.admin;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Collection;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.entity.Picture;
import org.jackysoft.service.PictureService;
import org.jackysoft.struts2.UtilsCRUDAction;
import org.jackysoft.util.HttpUtil;
import org.jackysoft.util.IOUtils;
import org.jackysoft.util.StringUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;



@Namespace("/picture")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class PictureAction extends UtilsCRUDAction<Picture> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5788960073640536589L;
	private PictureService service;
	private Picture bean;
	private Collection<Picture> beans;	
	
	public String view(){
		Picture pic = service.get(bean.getId());
		contentLength = pic.getSize();
		contentType = HttpUtil.extToMimeType(StringUtils.splitPostFileName(pic.getRealName()));
		upLoadFileName = StringUtils.toDownloadFileName(pic.getName());
		byte[] content =  IOUtils.toByteArray(new File(pic.getStorePath()));
		inputStream = new ByteArrayInputStream(content);
		return DOWNLOAD;
	}
	
	
	
	
	public void setBean(Picture bean){
	     this.bean = bean;
	}
	public Picture getBean(){
	     return this.bean;
	}
	public void setBeans(Collection<Picture> beans){
	
	     this.beans = beans;
	}
	public Collection<Picture> getBeans(){
	     return this.beans;
	}
	
	@Resource
	public void setService(PictureService service) {
		this.service = service;
		this.setDataProvider(service);
	}	
	
	public PictureService getService(){
	   return this.service;
	}
		
}